// UndstandDiaIdView.cpp : implementation file
//

#include "stdafx.h"
#include "UndstandDia.h"
#include "UndstandDiaDoc.h"
#include "CIDIASymb2Str.h"
#include "UndstandDiaIdView.h"
#include ".\undstanddiaidview.h"

#include "CIDIASymb2Str.h"

// CUndstandDiaIdView

IMPLEMENT_DYNCREATE(CUndstandDiaIdView, CListView)

CUndstandDiaIdView::CUndstandDiaIdView()
{
	//get the columnt number
	m_dwClmnNum=CIDIASymb2Str::GetPropertyNum();
	m_dwClmnNum++;	// add for the id column
}

CUndstandDiaIdView::~CUndstandDiaIdView()
{
}

BEGIN_MESSAGE_MAP(CUndstandDiaIdView, CListView)
	ON_WM_CONTEXTMENU()
	ON_NOTIFY_REFLECT(LVN_ENDLABELEDIT, OnLvnEndlabeledit)
END_MESSAGE_MAP()


// CUndstandDiaIdView diagnostics

#ifdef _DEBUG
void CUndstandDiaIdView::AssertValid() const
{
	CListView::AssertValid();
}

void CUndstandDiaIdView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}
#endif //_DEBUG


// CUndstandDiaIdView message handlers

void CUndstandDiaIdView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();

	// TODO: Add your specialized code here and/or call the base class
	GetDocument()->GetSession( &m_psession );
	GetDocument()->GetGlobal( &m_pglobal );

	CListCtrl& ListCtrl = GetListCtrl();

	// insert columns
	LV_COLUMN lvc;
	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvc.cx = 100;
	lvc.fmt = LVCFMT_LEFT;

	lvc.iSubItem = 0;
	lvc.pszText = _T("ID");
	ListCtrl.InsertColumn(0,&lvc);

	CString* pccstr=const_cast<CString*>(CIDIASymb2Str::GetPropertyS());
	for(int i = 1; i<m_dwClmnNum; i++)
	{
		lvc.iSubItem = i;
		lvc.pszText = pccstr[i-1].GetBuffer();
		ListCtrl.InsertColumn(i,&lvc);
	}

	ListCtrl.InsertItem(0, _T("Input id"));
}

BOOL CUndstandDiaIdView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Add your specialized code here and/or call the base class

	cs.style |= LVS_REPORT|LVS_EDITLABELS;
	return CListView::PreCreateWindow(cs);
}

void CUndstandDiaIdView::OnContextMenu(CWnd* pWnd, CPoint point)
{
	// TODO: Add your message handler code here
	HMENU hmenu;            // menu template          
	HMENU hmenuTrackPopup;  // shortcut menu   

	//  Load the menu template containing the shortcut menu from the 
	//  application's resources. 
	hmenu = LoadMenu(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDR_MENU_INSDEL)); 
	if (hmenu == NULL) 
		return; 

	// Get the first shortcut menu in the menu template. This is the 
	// menu that TrackPopupMenu displays. 
	hmenuTrackPopup = GetSubMenu(hmenu, 0); 

	// Draw and track the shortcut menu.  
	int iCmd=TrackPopupMenu(hmenuTrackPopup, TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RETURNCMD, 
		point.x, point.y, 0, pWnd->m_hWnd, NULL); 

	// Destroy the menu. 
	DestroyMenu(hmenu); 

	// insert item
	if(ID__INSERT==iCmd)
	{
		//insert a empty item
		CListCtrl& ListCtrl = GetListCtrl();
		int iItem=ListCtrl.GetNextItem(-1, LVNI_SELECTED);
		if(-1==iItem){iItem=0;}
		ListCtrl.InsertItem(iItem, _T("Input id"));
	}
	// delete item
	else if(ID__DELETE==iCmd)
	{
		CListCtrl& ListCtrl = GetListCtrl();

		POSITION pos = ListCtrl.GetFirstSelectedItemPosition();
		if (NULL!=pos)
		{
			while (pos)
			{
				int nItem = ListCtrl.GetNextSelectedItem(pos);
				ListCtrl.DeleteItem(nItem);
			}
		}
	}
}

void CUndstandDiaIdView::OnLvnEndlabeledit(NMHDR *pNMHDR, LRESULT *pResult)
{
	DWORD dwLowId, dwHighId;
	NMLVDISPINFO *pDispInfo = reinterpret_cast<NMLVDISPINFO*>(pNMHDR);
	// TODO: Add your control notification handler code here
	LVITEM* plvItem=&pDispInfo->item;

	dwLowId=dwHighId=0;
	_sntscanf(plvItem->pszText, _tcslen(plvItem->pszText), _T("%d:%d"), &dwLowId, &dwHighId);
	if(dwHighId<dwLowId)
	{
		dwHighId=dwLowId;
	}

	if(0==dwLowId)
	{
		*pResult = 0;
		return;
	}

	CListCtrl& cLstCtr=GetListCtrl();
	CHeaderCtrl* pcHdCtr=cLstCtr.GetHeaderCtrl();
	HDITEM hdi;
	TCHAR  chBuf[256];
	TCHAR  chId[256];
	hdi.mask = HDI_TEXT;
	hdi.pszText = chBuf;
	hdi.cchTextMax = 256;
	for(int i=dwLowId; i<=dwHighId; i++)
	{
		CComPtr<IDiaSymbol> psymbol;
		if(FAILED(m_psession->symbolById( i, &psymbol )))
		{
			TRACE( _T( "Can't find this id:%d, look the symbols from global view first!\r\n" ), i);
			MessageBox( _T( "Can't find this id, look the symbols from global view first!" ));
			break;
		}

		CIDIASymb2Str cSymb2Str(psymbol);
		
		itoa(i, chBuf, 10);
		cLstCtr.InsertItem(plvItem->iItem+i-dwLowId, chBuf);
		// insert subitems
		for(int j=1; j<m_dwClmnNum; j++)
		{
			pcHdCtr->GetItem(j, &hdi);
			cLstCtr.SetItemText(plvItem->iItem+i-dwLowId, j, cSymb2Str.PropertyVal(&CString(hdi.pszText)));
		}
	}

	*pResult = 1;
}
